package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.RequireLogin;
import cn.wolfcode.wolf2w.util.UserParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService iUserInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @RequestMapping("/query")
    public Object query(TravelQuery qo) {
        return JsonResult.success(travelService.query(qo));
    }


    /**
     * 游记页面编初始化
     *
     * @param id
     * @return
     */
    @RequestMapping("/input")
    public Object input(String id) {
        //根据id获取travel
        List<Destination> dests = destinationService.list();

        Map<String, Object> map = new HashMap<>();

        if (StringUtils.hasLength(id)) {
            Travel tv = travelService.get(id);
            map.put("tv", tv);
        }
        map.put("dests", dests);

        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = iUserInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());
        return JsonResult.success(travelService.saveOrUpdate(travel));
    }

    @RequestMapping("/getUser")
    public Object getUser(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    @RequestMapping("/updateUser")
    public Object updateUser(UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        Travel travel = travelService.get(id);
        return JsonResult.success(travel );
    }

    @RequestMapping("/comments")
    public Object comments(String travelId) {
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo, travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }

}
